<?php
/**************************************
 *
 * @services 服务层~主要进行数据筛选与过滤
 *
 **************************************/
namespace app\common\services;

use app\common\repositories\UserRepositories;
use GuzzleHttp\Client;
use think\config;
use think\Env;
use  think\Cache;
use think\Validate;
use think\Db;
class ProductServices
{
    protected $UserRepositories;

    protected $fileName = 'AppkeyConfig.php';

    protected $wechatSessionUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';

    public function __construct()
    {
        $this->UserRepositories = new UserRepositories();
    }

    public function getFligtList($respons)
    {
        $data['type'] = $respons['type'];
        $data['status'] = 1;
        return Db::name('product')
            ->field([
                'id',
                'title AS goodsTitle',
                'descs AS goodsText',
                'price AS goodsPrice',
                'images As goodsImgUrls',
                'info'
            ])->where($data)->paginate(10);

    }

    public function getFligtInfo($respons)
    {
        $rsl = Db::name('product')
            ->alias('c')
            ->field('a.*,b.*,a.id as pt_id')
            ->join('product_cost a', 'c.id = a.p_id', 'LEFT')
            ->join('product_trip b', 'c.id = b.p_id', 'LEFT')
            ->where('c.id', 'eq', $respons['p_id'])
            ->select();
        if (!$rsl) {
            return false;
        }
        $data['id'] = $rsl[0]['pt_id'];
        $data['p_id'] = $rsl[0]['p_id'];
        $data['traffic'] = $rsl[0]['traffic'];
        $data['shuttle_service'] = $rsl[0]['shuttle_service'];
        $data['accommo'] = $rsl[0]['accommo'];
        $data['have_meals'] = $rsl[0]['have_meals'];
        $data['guide_service'] = $rsl[0]['guide_service'];
        $data['other'] = $rsl[0]['other'];
        $data['personal'] = $rsl[0]['personal'];
        $data['single'] = $rsl[0]['single'];
        $data['children'] = $rsl[0]['children'];
        $data['insurance'] = $rsl[0]['insurance'];
        $keys = array_keys($data);
        unset($keys[0]);
        unset($keys[1]);
        foreach ($rsl as $k=> &$v) {
            $joins = array_keys($v);
            foreach ($joins as $vo) {
                if (in_array($vo, $keys)) {
                    unset($v[$vo]);
                    unset($v['pt_id']);
                }
            }
        }
        $msg['trip'] = $rsl;
        $msg['cost'] = $data;
        $info =         $msg['info'] = Db::name('product')->where(['id'=>$respons['p_id']])->value("info");

        $msg['info'] = empty($info)?[]:explode(",",$info);
            ;
        return $msg;
    }

    public function addProduct($respons)
    {
        $time = date('Y-m-d', time());
        $a = Cache::get($time);
        if (!$a) {
            Cache::set($time, 1, 3600 * 24);
        }else{
            Cache::inc($time);
        }
        $respons['create_time'] = time();
        $respons['trips_time']  = strtotime($respons['trips_time']);
        $rsl = Db::name('order_product')->insertGetId($respons);
        if ($rsl) {
            return $rsl;
        }
        return false;
    }

    public function selectOrder($respons)
    {
        $rsl = [];
        //产品订单
        if ($respons['type'] == 1) {
            $rsl = Db::name('order_product')
                ->alias('a')
                ->join('product b','a.p_id = b.id','LEFT')
                ->field('a.id,a.p_id, a.children_num as travelChildnum, a.adult_num as travelAdultnum, a.trips_time as travelDate, b.title as travelTitle, b.images as travelImgUrls, b.price')
                ->where('a.user_id', 'eq', $respons['user_id'])
                ->select();
        //机票订单
        } elseif($respons['type'] == 2) {
            $rsl = Db::name('order')
                ->alias('a')
                ->join('flight b','a.f_id = b.id','LEFT')
                ->field('a.id,a.f_id, a.num,b.trip, b.return_trip, b.trip_time,b.tto_time,b.rto_time, b.rt_time, b.tr_flight, b.price')
                ->where('a.user_id', 'eq', $respons['user_id'])
                ->select();

        }
        if ($rsl) {
            foreach ($rsl as &$v) {

                if($respons['type'] == 2){
                    $v['price'] = $v['price']*$v['num'];
                    $v['trip_time'] = date("Y年m月d日",$v['trip_time']);
                    $v['tto_time'] = date("H:i",$v['tto_time']);
                    $v['rt_time'] = date("Y年m月d日",$v['rt_time']);
                    $v['rto_time'] = date("H:i",$v['rto_time']);
                }else{
                    $v['travelDate'] = date("Y年m月d日",$v['travelDate']);
                }

            }
        }
        return $rsl;
    }

    public function bindCode($respons)
    {
        $data['mobile'] = $respons['mobile'];
        $data['code'] = $respons['code'];
        //查询短信表记录
        $codeData = Db::name('sms_log')->where($data)->find();
        if (!$codeData) {
            return ['error' =>true];
        }
        $map['id'] = $respons['id'];
        $msg = Db::name('wx_user')->where($map)->setField('mobile', $respons['mobile']);
        $userInfo =  Db::name('wx_user')->where($map)->find();
        Cache::set($respons['session_key'], $userInfo, 3600*24);
        if(!$msg){
            return false;
        }
        return ['success' =>true];
    }
}